package com.gitee.androidgoodies.batterylog;

import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.widget.Toast;

public class RecordServiceReceiver extends BroadcastReceiver {
    
    public static final String TAG = "RecordServiceReceiver";
    public final static String ACTION_REMOVE_RECORDITEM ="com.gitee.androidgoodies.batterylog.RecordServiceReceiver.ACTION_REMOVE_RECORDITEM";
    public final static String ACTION_ADD_RECORDITEM ="com.gitee.androidgoodies.batterylog.RecordServiceReceiver.ACTION_ADD_RECORDITEM";
    public final static String EXTRA_INT_BATTERY_PERCENTAGE = "EXTRA_INT_BATTERY_PERCENTAGE";
    
    public RecordServiceReceiver(){
        
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            int nLevel = intent.getIntExtra("level", 0);
            int nScale = intent.getIntExtra("scale", 100);
            int nBatteryPercentageNow = nLevel * 100 / nScale;
            RecordService.addRecordItem2(nBatteryPercentageNow);

        } else if (ACTION_REMOVE_RECORDITEM.equals(action)) {
            RecordService.removeAllRecordItem2();
        } else if (ACTION_ADD_RECORDITEM.equals(action)) {
            int nBatteryPercentageNow = intent.getIntExtra(EXTRA_INT_BATTERY_PERCENTAGE, 0);
            RecordService.addRecordItem2(nBatteryPercentageNow);
        }
        else{
            Toast.makeText(context, action, Toast.LENGTH_LONG).show();
        }
    }
    
}
